package com.harmontronics.hanma.paas.paas.domain.app.entity;

import com.harmontronics.hanma.paas.paas.consts.DbConstants;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.Fields;
import com.harmontronics.hanma.paas.paas.utils.StringUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Title:
 * @ClassName: TreeeNode
 * @Description:
 * @Author: tylr
 * @Date: 2021/6/8 9:44
 * @Version: 1.0.0
 */
@Data
@NoArgsConstructor
public class TreeNode {

    /**
     * 节点唯一键
     */
    private String uniqueKey;

    /**
     * 标签内容
     */
    private String label;

    /**
     * 排序
     */
    private Long serialNum;

    /**
     * 节点数据
     */
    private Map<String,Object> data;

    /**
     * 子节点
     */
    private List<TreeNode> children;


    public static List<TreeNode> getChildren(List<Map<String, Object>> allNodesInTree,Long parentId){
        List<TreeNode> children = new ArrayList<>();
        final String parentIdStr = String.valueOf(parentId);
        allNodesInTree.forEach(item->{
            if(item.containsKey(Fields.PARENT_ID) && item.get(Fields.PARENT_ID) != null
                    && StringUtil.equals(item.get(Fields.PARENT_ID).toString(),parentIdStr)){
                TreeNode treeNode = new TreeNode();
                treeNode.setUniqueKey(item.get(DbConstants.ID).toString());
                //treeNode.setLabel(item);
                //reeNode.setSerialNum(item);
                treeNode.setData(item);
                treeNode.setChildren(getChildren(allNodesInTree,Long.valueOf(treeNode.getUniqueKey())));
                children.add(treeNode);
            }
        });
        return children;
    }
}
